﻿using System;
using FortuneUniverse.Fortune.Algorithms;

namespace FortuneUniverse.Fortune.Dyson
{
  public class PlanetAlgorithm9 : PlanetAlgorithm
  {
    public override void GenerateTerrain(double modX, double modY)
    {
      double num1 = 0.01;
      double num2 = 0.012;
      double num3 = 0.01;
      double num4 = 3.0;
      double num5 = -0.2;
      double num6 = 0.9;
      double num7 = 0.5;
      URandom1 urandom1 = new URandom1(this.planet.seed);
      int seed1 = urandom1.Next();
      int seed2 = urandom1.Next();
      SimplexNoise simplexNoise1 = new SimplexNoise(seed1);
      SimplexNoise simplexNoise2 = new SimplexNoise(seed2);
      PlanetRawData data = this.planet.data;
      for (int index = 0; index < data.dataLength; ++index)
      {
        double num8 = (double) data.vertices[index].x * (double) this.planet.radius;
        double num9 = (double) data.vertices[index].y * (double) this.planet.radius;
        double num10 = (double) data.vertices[index].z * (double) this.planet.radius;
        double num11 = simplexNoise1.Noise3DFBM(num8 * num1 * 0.75, num9 * num2 * 0.5, num10 * num3 * 0.75, 6) * num4 + num5;
        double num12 = simplexNoise2.Noise3DFBM(num8 * (1.0 / 400.0), num9 * (1.0 / 400.0), num10 * (1.0 / 400.0), 3) * num4 * num6 + num7;
        double num13 = num12 > 0.0 ? num12 * 0.5 : num12;
        double num14 = num11 + num13;
        double f = num14 > 0.0 ? num14 * 0.5 : num14 * 1.6;
        double num15 = (f > 0.0 ? Maths.Levelize3(f, 0.7) : Maths.Levelize2(f, 0.5)) + 0.618;
        double num16 = num15 > -1.0 ? num15 * 1.5 : num15 * 4.0;
        double num17 = simplexNoise1.Noise3DFBM(num8 * num1 * modX, num9 * num2 * modX, num10 * num3 * modX, 6) * num4 + num5;
        double num18 = simplexNoise2.Noise3DFBM(num8 * (1.0 / 400.0), num9 * (1.0 / 400.0), num10 * (1.0 / 400.0), 3) * num4 * num6 + num7;
        double num19 = num18 > 0.0 ? num18 * 0.5 : num18;
        double num20 = Math.Pow((num17 + num19 + 5.0) * 0.13, 6.0) * 24.0 - 24.0;
        double num21 = num16 >= -modY ? 0.0 : Math.Pow(Math.Min(Math.Abs(num16 + modY) / 5.0, 1.0), 1.0);
        double num22 = num16 * (1.0 - num21) + num20 * num21;
        double num23 = num22 > 0.0 ? num22 * 0.5 : num22;
        data.heightData[index] = (ushort) (((double) this.planet.radius + num23 + 0.2) * 100.0);
      }
    }
  }
}
